#include "stm32f10x.h"                 
#include "Delay.h"
#include "OLED.h"
#include "Timer3.h"
#include "Timer2.h"
#include "HCSR04.h"
#include "PWM.h"
#include "BEEP.h"
#include "Serial.h"
#include "Serial_usart1.h"
#include "OLED_word.h"
#include "Key.h"
#include "LED_BEEP_Work.h"
#include "voice_module.h"



uint8_t menu_Flag=1;  
uint8_t limit_Flag;
uint8_t lower_Flag;


int main(void)
{	

	OLED_Init();		
	Key_Init();
	Timer3_Init();		
	Timer2_Init();
	HC_SR04_Init();		
	PWM_Init();
	BEEP_Init();

	Voice_Init();
	Serial_Init();
	Serial_usart1_Init();
//	OLED_ShowString(0, 0, "Distance:", OLED_8X16);
	GPIO_ResetBits(GPIOB,GPIO_Pin_3);
	Delay_ms(500);
	GPIO_SetBits(GPIOB,GPIO_Pin_3);
	OLED_Update();
	while (1)
	{

				while(menu_Flag==1)
				{

	  	  	Ranging_mode();
					 KeyNum=Key_GetNum();
					
					 if(Key_Num1==1){KeyNum=1;}
					 if(Key_Num1==2){KeyNum=2;}
					 if(Key_Num1==3){KeyNum=3;}
					 if(Key_Num1==4){KeyNum=4;}				
					 
					 if(KeyNum==1){KeyNum=0;Key_Num1=0;menu_Flag++;}

            while(menu_Flag==2)
						{
							Distan_TH1_SET_mode();
							KeyNum=Key_GetNum();
					    if(Key_Num1==1){KeyNum=1;}
              if(Key_Num1==2){KeyNum=2;}
              if(Key_Num1==3){KeyNum=3;}
              if(Key_Num1==4){KeyNum=4;}	
							
							if(KeyNum==1){KeyNum=0;Key_Num1=0;menu_Flag=1;}	
							else if(KeyNum==2){KeyNum=0;Key_Num1=0;limit_Flag++;}
              else if(KeyNum==3){KeyNum=0;Key_Num1=0;Distance_mm_LED_PWM+=10;}
							else if(KeyNum==4){KeyNum=0;Key_Num1=0;Distance_mm_LED_PWM-=10;}							
							
							while(limit_Flag==1)
								{
										Distan_TH2_SET_mode();
										KeyNum=Key_GetNum();
										if(Key_Num1==1){KeyNum=1;}
										if(Key_Num1==2){KeyNum=2;}
										if(Key_Num1==3){KeyNum=3;}
										if(Key_Num1==4){KeyNum=4;}	
										if(KeyNum==1){KeyNum=0;Key_Num1=0;menu_Flag=1;limit_Flag=0;}	
										else if(KeyNum==2){KeyNum=0;Key_Num1=0;limit_Flag=0;menu_Flag=2;}
										else if(KeyNum==3){KeyNum=0;Key_Num1=0;Distance_mm_BEEP+=10;}
										else if(KeyNum==4){KeyNum=0;Key_Num1=0;Distance_mm_BEEP-=10;}	

									}

						}
		  	}
	}
}


void TIM2_IRQHandler(void)
{
	if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
	{
		Key_Tick();
	  Distance_DET_Tick();
		BEEP_Tick();
		LED_Tick();
		Serial_vlue++;
		if(Serial_vlue>=100)
		{
			Serial_Num++;
			Serial_vlue=0;
		}
		
		TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
	}
}


